﻿'Bram Debouvere
Option Strict On

Imports Gift.BL
Imports Gift.BL.Validation

Public Class frmGiftListWizard

    Private _giftList As GiftList
    Private _isEdit As Boolean
    Private _huidig As Integer = 1


    Public Property GiftListProductItems As List(Of GiftListProduct)
        Get
            Dim list As New List(Of GiftListProduct)
            For Each lvi As FilteredListViewItem In flvProductenInGeschenkenLijst.Items
                Dim glpi As GiftListProduct = CType(lvi.TaggedObject, GiftListProduct)
                glpi.Giftlist = _giftList
                list.Add(glpi)
            Next
            Return list
        End Get
        Set(ByVal Value As List(Of GiftListProduct))
            flvProductenInGeschenkenLijst.Items.Clear()
            For Each glp As GiftListProduct In Value
                flvProductenInGeschenkenLijst.Items.Add(New FilteredListViewItem({glp.Product.Naam, glp.Price.ToString, glp.Amount.ToString, glp.Category}, glp))
            Next
            flvProductenInGeschenkenLijst.FillListItems()
        End Set
    End Property



    Public Property GiftList As GiftList
        Get
            Return _giftList
        End Get
        Set(ByVal Value As GiftList)
            'reset form and put values
            _giftList = Value
            initializeWizard()
        End Set
    End Property

    Public Property IsEdit As Boolean
        Get
            Return _isEdit
        End Get
        Set(ByVal Value As Boolean)
            _isEdit = Value
            cboEigenaar.Enabled = Not Value
        End Set
    End Property

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        'put items in the properties
        _giftList.Naam = txtNaam.Text
        _giftList.LijstCode = txtCode.Text
        _giftList.LijstActief = chkActief.Checked
        Dim occ As Occasion = CType(cboGelegenheid.SelectedItem, Occasion)
        _giftList.GelegenheidId = occ.Id
        _giftList.GelegenheidNaam = occ.Name
        Dim person As Person = CType(cboEigenaar.SelectedItem, Person)
        _giftList.LijstPerId = CInt(person.Id)
        _giftList.PerNaam = person.Naam

        'show the user his list
        showListDetails()

        'return dialogresult
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Public Sub showListDetails()
        'show the user his list
        Dim str As String = "UW GESCHENKENLIJST:" & vbCrLf & vbCrLf & "Naam: " & _giftList.Naam & vbCrLf & "Persoon: " & _giftList.PerNaam & vbCrLf & _
                        "Geheime code voor webtoegang: " & _giftList.LijstCode & vbCrLf & vbCrLf & "Items:" & vbCrLf
        For Each glp As GiftListProduct In GiftListProductItems
            str &= "- " & glp.Product.Naam & "(aantal: " & glp.Amount.ToString & "   kostprijs: " & glp.Price.ToString() & ")" & vbCrLf
        Next
        MessageBox.Show(str, "Lijst", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnAnnuleren_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuleren.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnVorige_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVorige.Click
        _huidig -= 1
        GetCurrentPanel()
    End Sub

    Private Sub btnVolgende_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVolgende.Click
        If (checkValid()) Then
            _huidig += 1
            GetCurrentPanel()
        End If
    End Sub

    Private Sub GetCurrentPanel()
        Select Case _huidig
            Case 1
                pnlGeschenkLijst1.Show()
                pnlGeschenkLijst2.Hide()
                pnlGeschenkLijst3.Hide()
                btnVorige.Enabled = False
                btnVolgende.Enabled = True
            Case 2
                pnlGeschenkLijst1.Hide()
                pnlGeschenkLijst2.Show()
                pnlGeschenkLijst3.Hide()
                btnVorige.Enabled = True
                btnVolgende.Enabled = True
            Case 3
                pnlGeschenkLijst1.Hide()
                pnlGeschenkLijst2.Hide()
                pnlGeschenkLijst3.Show()
                btnVorige.Enabled = True
                btnVolgende.Enabled = False
                If (IsEdit) Then
                    btnOk.Enabled = True
                End If

        End Select
    End Sub

    Private Function checkValid() As Boolean
        Dim isValid As Boolean = True
        'check current panel
        Select Case _huidig
            Case 1
                If cboGelegenheid.SelectedIndex = -1 Or txtNaam.Text = "" Or txtCode.Text = "" Then
                    isValid = False
                    MessageBox.Show("Vul alles in om door te gaan", "Vul alles in", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                End If
                If GiftList.codeEqualsOtherGiftList(txtCode.Text, _giftList, frmMain.dbh) Then
                    isValid = False
                    MessageBox.Show("Deze code bestaat al, kies een andere", "Code bestaat al", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                End If
                If GiftList.nameEqualsOtherGiftList(txtNaam.Text, _giftList, frmMain.dbh) Then
                    isValid = False
                    MessageBox.Show("Deze naam bestaat al, kies een andere", "Naam bestaat al ", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                End If
                Return isValid
            Case 2
                If (flvProductenInGeschenkenLijst.Items.Count = 0) Then
                    isValid = False
                    MessageBox.Show("Voeg items toe", "Geen items", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                End If
                Return isValid
            Case 3
                'full check finishes here
                If cboEigenaar.SelectedIndex = -1 Or cboEigenaar.SelectedItem Is Nothing Then
                    isValid = False
                    MessageBox.Show("Selecteer een eigenaar voor de geschenkenlijst", "Geen eigenaar", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                End If
                Return isValid
        End Select
        Return True
    End Function

    Public Sub initializeWizard()
        _huidig = 1
        flvProductenInGeschenkenLijst.Colums = {"Naam", "Prijs", "Aantal", "Categorie"}
        GetCurrentPanel()
        AddOccasions()
        AddPersons()
        AddCategories()

        txtCode.Text = _giftList.LijstCode
        txtNaam.Text = _giftList.Naam
        chkActief.Checked = _giftList.LijstActief

        setOccasionToCurrent()
        setProductsToCurrent()
        setGiftListProductToCurrent()
        setPersonsToCurrent()
    End Sub

    Private Sub AddOccasions()
        Dim oList As List(Of Occasion) = Occasion.SelectAllOccasions(frmMain.dbh)
        cboGelegenheid.Items.Clear()
        For Each occasionItem As Occasion In oList
            cboGelegenheid.Items.Add(occasionItem)
        Next
    End Sub

    Private Sub setOccasionToCurrent()
        'check for new occasion
        If _giftList.GelegenheidId <> -1 Then
            For Each occasionItem As Occasion In cboGelegenheid.Items
                If occasionItem.Id = _giftList.GelegenheidId Then
                    cboGelegenheid.SelectedItem = occasionItem
                    Exit Sub 'stop the looping
                End If
            Next
        End If
    End Sub


    Private Sub setProductsToCurrent()

        Dim pList As List(Of Product) = Product.GetProductsFromDatabase(frmMain.dbh)
        flvProducten.Items.Clear()
        flvProducten.Colums = {"Naam", "Omschrijving", "Prijs", "Beschikbaar"}
        For Each productObject As Product In pList
            Dim objectAmountArray() As Object = {productObject, 0} 'houd het object + het aantal geselecteerde items vast
            flvProducten.Items.Add(New FilteredListViewItem({productObject.Naam, productObject.Omschrijving, CStr(productObject.Prijs), CStr(IIf(productObject.Onbeschikbaar, "Nee", "Ja"))}, objectAmountArray))

        Next
        flvProducten.FillListItems()
    End Sub

 

    Private Sub flvProducten_SelectedIndexChanged() Handles flvProducten.SelectedIndexChanged
        If Not flvProducten.SelectedItem Is Nothing Then
            'get aantal from selected product
            Dim arrObject() As Object = DirectCast(flvProducten.SelectedObject, Object())

            nudAantal.Tag = flvProducten.SelectedItem
            nudAantal.Value = CInt(arrObject(1))
        End If
    End Sub

    Private Sub AddItem()
        'no pointers in vb.net --> alternative: loop to find object that was selected
        For Each item As FilteredListViewItem In flvProducten.Items
            If (ReferenceEquals(nudAantal.Tag, item)) Then
                Dim obj() As Object = CType(item.TaggedObject, Object()) 'get object from tag
                Dim prod As Product = CType(obj(0), Product) 'get product from object
                Dim glp As New GiftListProduct(prod, _giftList, cboCategorie.SelectedItem.ToString, prod.Prijs * nudAantal.Value, CInt(nudAantal.Value))
                If Not glpAlreadyExists(glp) Then
                    flvProductenInGeschenkenLijst.Items.Add(New FilteredListViewItem({glp.Product.Naam, glp.Price.ToString, glp.Amount.ToString, glp.Category}, glp))
                    flvProductenInGeschenkenLijst.FillListItems()
                Else
                    MessageBox.Show("Dit product staat al op je lijst")
                End If
            End If
        Next


    End Sub

    Private Sub btnVoegToe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVoegToe.Click
        If Not flvProducten.SelectedItem Is Nothing AndAlso Not cboCategorie.SelectedItem Is Nothing AndAlso Not cboCategorie.SelectedItem.ToString = "" AndAlso Not nudAantal.Value < 1 Then
            AddItem()
        Else
            MessageBox.Show("Kies een item en een categorie, aantal moet 1 of meer zijn!", "Niet alles ingevuld")
        End If
    End Sub

    Private Sub btnNieuweCategorie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNieuweCategorie.Click
        Dim str As String = InputBox("Geef de naam voor een nieuwe categorie", "Naam nieuwe categorie")
        If (str <> "") Then
            If (str.Length >= 20) Then
                MessageBox.Show("Uw categorienaam is te lang!")
                Exit Sub
            End If
            'look if category already exists
            For Each item As String In cboCategorie.Items
                If str = item Then
                    MessageBox.Show("Die categorie bestaat al!")
                    Exit Sub
                End If
            Next
            'if it doesn't exists - enter it into the combobox
            cboCategorie.Items.Add(str)
        End If

    End Sub

    Private Function glpAlreadyExists(ByVal glp As GiftListProduct) As Boolean
        Dim exists As Boolean = False
        For Each item As FilteredListViewItem In flvProductenInGeschenkenLijst.Items
            If CType(item.TaggedObject, GiftListProduct).Product.Id = glp.Product.Id Then
                exists = True
            End If
        Next
        Return exists
    End Function

    Private Sub btnVerwijder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerwijder.Click
        If Not flvProductenInGeschenkenLijst.SelectedItem Is Nothing Then
            If Not _isEdit OrElse Not GiftListProduct.isItemBesteld(CType(flvProductenInGeschenkenLijst.SelectedObject, GiftListProduct), frmMain.dbh) Then
                flvProductenInGeschenkenLijst.Items.Remove(flvProductenInGeschenkenLijst.SelectedItem)
                flvProductenInGeschenkenLijst.FillListItems()
            Else
                MessageBox.Show("Dit item werd reeds besteld en kan dus niet verwijderd worden!", "Onmogelijk", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            End If
        End If
    End Sub

    Private Sub setPersonsToCurrent()
        'check for new person
        If _giftList.LijstPerId <> -1 Then
            For Each item As Person In cboEigenaar.Items
                If CInt(item.Id) = _giftList.LijstPerId Then
                    cboEigenaar.SelectedItem = item
                    Exit Sub 'stop the looping
                End If
            Next
        End If
    End Sub

    Private Sub AddPersons()
        cboEigenaar.Items.Clear()
        Dim list As List(Of Person) = Person.GetPersonsFromDatabase(frmMain.dbh)
        For Each item As Person In list
            cboEigenaar.Items.Add(item)
        Next
    End Sub

    Private Sub setGiftListProductToCurrent()
        'reset
        flvProductenInGeschenkenLijst.Items.Clear()
        flvProductenInGeschenkenLijst.FillListItems()
    End Sub

    Private Sub cboEigenaar_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEigenaar.SelectedIndexChanged
        If _huidig = 1 Then
            Exit Sub
        End If
        If checkValid() Then
            btnOk.Enabled = True
        End If
    End Sub

    Private Sub AddCategories()
        cboCategorie.Items.Clear()
        Dim categories As List(Of String) = GiftListProduct.getCategories(frmMain.dbh)
        For Each category As String In categories
            cboCategorie.Items.Add(category)
        Next
    End Sub

    'aantal aanpassen (eerst controleren of aantal bv wel verlaagd mag worden)
    Private Sub btnAantal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAantal.Click
        If Not flvProductenInGeschenkenLijst.SelectedItem Is Nothing Then
            Dim inputString As String = InputBox("Geef een nieuw maximum aantal", "Verander aantal", "1")
            If (checkGiftListWizardAantalInputString(inputString)) Then
                If Not _isEdit OrElse Not GiftListProduct.isItemBesteld(DirectCast(flvProductenInGeschenkenLijst.SelectedItem.TaggedObject, GiftListProduct), frmMain.dbh) Then
                    Dim o As GiftListProduct = CType(flvProductenInGeschenkenLijst.SelectedItem.TaggedObject, GiftListProduct)
                    o.Amount = CInt(inputString)
                    o.Price = o.Product.Prijs * o.Amount
                    flvProductenInGeschenkenLijst.SelectedItem.TaggedObject = o 'update the listviewitem
                    flvProductenInGeschenkenLijst.SelectedItem.SubItems(0).Text = o.Product.Naam
                    flvProductenInGeschenkenLijst.SelectedItem.SubItems(1).Text = o.Price.ToString
                    flvProductenInGeschenkenLijst.SelectedItem.SubItems(2).Text = o.Amount.ToString
                    flvProductenInGeschenkenLijst.SelectedItem.SubItems(3).Text = o.Category
                    flvProductenInGeschenkenLijst.FillListItems()
                Else
                    Dim amountOrdered As Integer = GiftListProduct.getAmountOrdered(DirectCast(flvProductenInGeschenkenLijst.SelectedItem.TaggedObject, GiftListProduct), frmMain.dbh)
                    If CInt(inputString) >= amountOrdered Then
                        Dim o As GiftListProduct = CType(flvProductenInGeschenkenLijst.SelectedItem.TaggedObject, GiftListProduct)
                        o.Amount = CInt(inputString)
                        o.Price = o.Product.Prijs * o.Amount
                        flvProductenInGeschenkenLijst.SelectedItem.TaggedObject = o 'update the listviewitem
                        flvProductenInGeschenkenLijst.SelectedItem.SubItems(0).Text = o.Product.Naam
                        flvProductenInGeschenkenLijst.SelectedItem.SubItems(1).Text = o.Price.ToString
                        flvProductenInGeschenkenLijst.SelectedItem.SubItems(2).Text = o.Amount.ToString
                        flvProductenInGeschenkenLijst.SelectedItem.SubItems(3).Text = o.Category
                        flvProductenInGeschenkenLijst.FillListItems()
                    Else
                        MessageBox.Show("Je kunt het aantal niet zo laag zetten, want" & vbCrLf & "er zijn reeds " & amountOrdered.ToString & " items besteld.")
                    End If
                End If
            Else
                MessageBox.Show("Geef een aantal in tussen 1 en 100")
            End If
        End If
    End Sub

    
    Private Sub btnImporteren_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImporteren.Click
        ImportFromExcel()
    End Sub

    Private Sub ImportFromExcel()
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim list As List(Of GiftListProduct) = GiftList.importFromExcel(ofd.FileName, _giftList, My.Application.Info.DirectoryPath, frmMain.dbh)
            'giftlist to the view
            For Each glp As GiftListProduct In list
                If Not glpAlreadyExists(glp) Then
                    flvProductenInGeschenkenLijst.Items.Add(New FilteredListViewItem({glp.Product.Naam, glp.Price.ToString, glp.Amount.ToString, glp.Category}, glp))
                    flvProductenInGeschenkenLijst.FillListItems()
                Else
                    MessageBox.Show("Een product dat je probeert te importeren staat al op je lijst, het is niet nogmaals toegevoegd", "Product al aanwezig", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Next

            MessageBox.Show("De geschenkenlijst werd geïmporteerd", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If


    End Sub
End Class